We first read in a data set representing several different values having to due with the city of Philadelphia. The data, called “Philly”, contains information on Shootings that have occurred in the area and categorizes them as either “Fatal” or “Non-Fatal”. “Philly” has 15555 observations and 21 total variables.
philly <- na.omit(st_read("https://pengdsci.github.io/STA553VIZ/w08/PhillyShootings.geojson"))
phillyNeighbor <- st_read("https://pengdsci.github.io/STA553VIZ/w08/Neighborhoods_Philadelphia.geojson")
head(philly)
Simple feature collection with 6 features and 21 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -75.10962 ymin: 40.01166 xmax: -75.08552 ymax: 40.01489
Geodetic CRS: WGS 84
objectid year dc_key code date_ time race sex age
1 12728033 2016 201615054780.0 400 2016-06-06 20:00:00 12:15:00 B M 19
2 12728034 2016 201615117555.0 300 2016-12-03 19:00:00 05:43:00 B M 38
3 12728035 2018 201815093657.0 400 2018-10-13 20:00:00 21:02:00 B M 31
4 12728036 2020 202015094989.0 400 2020-12-01 19:00:00 17:12:00 B M 23
5 12728037 2018 201824100255.0 400 2018-11-08 19:00:00 04:29:00 B M 27
6 12728038 2019 201924106228.0 400 2019-10-27 20:00:00 04:10:00 W M 22
wound officer_involved offender_injured offender_deceased
1 Hand N N N
2 Chest N N N
3 Multiple N N N
4 Hand N N N
5 Arm N N N
6 Multiple N N N
location latino point_x point_y dist inside outside
1 4600 BLOCK Frankford Ave 0 -75.08552 40.01489 15 0 1
2 4600 BLOCK Frankford Ave 0 -75.08552 40.01489 15 0 1
3 4600 BLOCK Frankford Ave 0 -75.08552 40.01489 15 0 1
4 4600 BLOCK FRANKFORD AVE 0 -75.08552 40.01489 15 0 1
5 900 BLOCK E Hunting Park Ave 0 -75.10962 40.01167 24 0 1
6 900 BLOCK E HUNTING PARK AVE 1 -75.10962 40.01167 24 0 1
fatal geometry
1 0 POINT (-75.08552 40.01489)
2 0 POINT (-75.08552 40.01489)
3 1 POINT (-75.08552 40.01489)
4 0 POINT (-75.08552 40.01489)
5 0 POINT (-75.10962 40.01167)
6 0 POINT (-75.10962 40.01167)
Now, we can create a leaflet map looking at fatal versus non-fatal crimes that occured in Philadelphia by using the “color” function once again. The color is dependent on whether or not a crime was labeled as “Fatal” or “Nonfatal”. So, each category will have a specific color, representing the type of crime (fatal vs. nonfatal) that occurred at the time. Each crime location is represented with a circle marker. Hovering over a point will display information for “Object ID”, “Year”, “Race”, “Sex”, “Age”, “Wound”, and “Location” for each of the crime points.
# Load required libraries
library(leaflet)
library(sf)
# Convert 'philly' data to sf object
philly_sf <- st_as_sf(philly, coords = c("point_x", "point_y"), crs = 4326)
# Define color palette for fatal and non-fatal crimes
fatal_color <- "red"
non_fatal_color <- "gold"
# Create leaflet map
map <- leaflet() %>%
addProviderTiles(providers$Esri.WorldGrayCanvas) %>%
addPolygons(data = phillyNeighbor,
color = 'skyblue',
weight = 1) %>%
addCircleMarkers(data = philly_sf,
~point_x, ~point_y,
color = ifelse(philly$fatal == 1, fatal_color, non_fatal_color),
radius = 5,
popup = ~paste("Object ID: ", objectid,
"<br>Year: ", year,
"<br>Race: ", race,
"<br>Sex: ", sex,
"<br>Age: ", age,
"<br>Wound: ", wound,
"<br>Location: ", location),
labelOptions = labelOptions(
direction = "auto"
)
) %>%
addLegend(
position = "bottomright",
colors = c("red", "gold"),
labels = c("Fatal", "Non-Fatal"),
title = "Crime Type"
) %>%
addScaleBar() %>%
addControl(
html = "<h4>Philadelphia Crime Locations (2015-2024)</h4>",
position = "topright"
) %>%
addProviderTiles(providers$Esri.WorldGrayCanvas) %>%
setView(lng = -75.1527, lat = 39.9707, zoom = 11)
# Display the map
map